
//初始化【边框颜色】【背景颜色】
var initBorderColor = "#515151";
var initBgColor = "#D9D9D9";

//动画回放【边框颜色】【背景颜色】
var historyBorderColor = "#f55b5b";
var historyBgColor = "#ffe6e6";

var currentBorderColor = "#f9a509";
var currentBgColor = "#feeac5";

//鼠标覆盖时【边框颜色】【背景颜色】
var overBorderColor = "#8080ff";
var overBgColor = "#d7d7ff";

/**
 * 
 * @param params 
 * 	例如：
 * 			`{"type":"UserTask", "mission":"mission274341","x":204,"y":158,"w":120,"h":50, "desc":"请假申请"}`
 * 			`{"type":"StartEvent", "mission":"mission275037","x":235,"y":51,"w":58,"h":58, "desc":"开始"}`
 * 
 * @param workflowDiv
 * 	workflow DOM节点
 * 
 */
function UserTask(params, workflowDiv) {
	
	//类型
	this.type = params.type;
	//mission名称
	this.missionName = params.mission;
	//描述
	this.desc = params.desc;
	//x、y坐标 w、h宽度高度
	this.x = params.x + 4;
	this.y = params.y + 4;
	this.w = params.w - 6;
	this.h = params.h - 6;
	
	this.workflowDiv = workflowDiv;
}

UserTask.prototype = {
	
	//构造函数
	constructor : UserTask,
	
	//图片节点
	circleStrs :  ['StartEvent', 'EndEvent', 'ForkBefore', 'ForkAfter', 'LaneBefore', 'LaneAfter', 'SubStartEvent', 'SubEndEvent'], 
	
	//div内容
	node : null,
	
	isOver : false,
	isCurrent: false,
	isHistory : false,
	// 边框颜色
	borderColor : null,
	// 背景颜色
	bgColor : null,
	// 图片后缀
	imageSuffix : '',

	show : function() {
		
		var node = null;
		
		if(this.circleStrs.contains(this.type)) {
			node = this.initCircle();
		} else {
			node = this.initTask();
		}
		node.bind("mouseover", this, this.over);
		node.bind("mouseout", this, this.out);
		this.node = node;
		
		this.workflowDiv.append(node);
	},

	initTask : function() {
		if(this.isCurrent) {
			this.bgColor = currentBgColor;
			this.borderColor = currentBorderColor;
			this.imageSuffix = "His1";
		} else {
			this.bgColor = initBgColor;
			this.borderColor = initBorderColor;
		}
		
		var innerNode = $("<div></div>");
		innerNode.attr("class", "userTask-inner");
		innerNode.css("padding-left", "30px");
		innerNode.css("line-height", (this.h - 8) + "px");
		innerNode.css("background-color", this.bgColor);
		innerNode.css("background-image", "url(wf/img/" + this.type + this.imageSuffix + ".png)");
		innerNode.css("color", this.borderColor);
		innerNode.html(this.desc);
		
		var node = $("<div></div>");
		node.css("top", this.y + 2);
		node.css("left", this.x + 2);
		node.css("width", this.w - 6);
		node.css("height", this.h - 6);
		node.css("background-color", this.borderColor);
		node.css("line-height", this.h - 6);
		node.attr("class", "userTask");
		
		node.append(innerNode);
		node.innerNode = innerNode;
		return node;
	},
	
	initCircle : function() {
		var node = $('<img style="position: absolute;" />');
		node.css("top", this.y + 3);
		node.css("left", this.x + 2);
		node.css("width", 46);
		node.css("height", 46);
		node.css("cursor", "pointer");
		node.attr("src", "wf/img/" + this.type + ".png");
		return node;
	},
	
	historyProcess : function() {
		this.isHistory = true;
		if(!this.circleStrs.contains(this.type)) {
			this.setColor(historyBorderColor, historyBgColor);
			this.node.innerNode.css("background-image", "url(wf/img/" + this.type + "His.png)");
			this.node.innerNode.css("color", historyBorderColor);
		} else {
			this.node.attr("src", "wf/img/" + this.type + "His.png");
		}
	},
	
	setColor : function(borderColor, bgColor) {
		var $this = this.node;
		$this.css("background-color", borderColor);
		$this.innerNode.css("background-color", bgColor);
	},
	
	reset : function() {
		
		this.isHistory = false;
			
		if(!this.circleStrs.contains(this.type)) {
			this.setColor(this.borderColor, this.bgColor);
			this.node.innerNode.css("background-image", "url(wf/img/" + this.type + this.imageSuffix + ".png)");
			this.node.innerNode.css("color", this.borderColor);
		} else {
			this.node.attr("src", "wf/img/" + this.type + this.imageSuffix + ".png");
		}
		
	},

	over : function(event) {
		
		var $this = event.data;
		
		if($this.isCurrent) return ;
		
		$this.bgColor = overBgColor;
		$this.borderColor = overBorderColor;
		$this.imageSuffix = "Over";
		
		if(!$this.isHistory) {
			$this.isOver = true;
			if($this.node.innerNode) {
				$this.setColor(overBorderColor, overBgColor);
				$this.node.innerNode.css("background-image", "url(wf/img/" + $this.type + "Over.png)");
				$this.node.innerNode.css("color", "#7777e0");
			} else {
				$this.node.attr("src", "wf/img/" + $this.type + "Over.png");			
			}
		}
		
	},

	out : function(event) {
		
		var $this = event.data;
		
		if($this.isCurrent) return ;
		
		$this.bgColor =  initBgColor;
		$this.borderColor = initBorderColor;
		$this.imageSuffix = "";
		
		if(!$this.isHistory) {
			$this.isOver = false;
			if($this.node.innerNode) {
				$this.setColor(initBorderColor, initBgColor);
				$this.node.innerNode.css("background-image", "url(wf/img/" + $this.type + ".png)");
				$this.node.innerNode.css("color", "#515151");
			} else {
				$this.node.attr("src", "wf/img/" + $this.type + ".png");			
			}
		}
		
	}

}


 Array.prototype.contains = function(item) {
	for (i = 0; i < this.length; i++) {
		if (this[i] == item) {
			return true;
		}
	}
	return false;
};